<?php
/**
 * Created by PhpStorm.
 * User: ztos
 * Date: 2017/11/16
 * Time: 下午2:02
 */

namespace app\admin\controller;
use app\common\model\PartyBuilding as PartyBuildingModel;
use app\common\model\Banner as BannerModel;
class Banner extends Admin
{
    /**
     * 轮播图首页
     */
    public function index()
    {
        $map = ['status' => 0];
        $list = BannerModel::where($map)->order('id desc')->paginate();
        $this->assign('list', $list);

        return $this->fetch();
    }

    /**
     * 添加banner
     */
    public function add()
    {
        if (IS_POST) {
            $partyBuilding = new BannerModel();
            if (input('id')) {

                $result = $partyBuilding->save($_POST, ['id' => input('id')]);
                if ($result) {

                    return $this->success("修改成功！");
                } else {

                    return $this->error("修改失败");
                }
            } else {
                $_POST['create_time'] = time();
                $_POST['status'] = 0;
                unset($_POST['id']);
                $result = $partyBuilding->save($_POST);
                if ($result) {

                    return $this->success("添加成功！");
                } else {

                    return $this->error("添加失败");
                }
            }

        } else {
            $news = BannerModel::where('id', 'eq', input('id'))->find();
            $this->assign('news', $news);
            return $this->fetch();
        }
    }

    /**
     * 删除
     */
    public function moveToTrash() {
        $ids = input('ids/a');
        $result = BannerModel::where('id', 'in', $ids)->update(['status' => -1]);

        if($result) {

            return $this->success('删除成功');

        } elseif(!$result) {

            return $this->error('删除失败');
        }
    }



}